package com.jeecg.p3.system.poster.service;

import com.jeecg.p3.system.poster.dao.BlackPersonDao;
import com.jeecg.p3.system.poster.dao.PosterManagerDao;
import com.jeecg.p3.system.poster.entity.BlackPerson;
import com.jeecg.p3.system.poster.entity.PosterManager;
import org.jeecgframework.p3.core.utils.common.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 * @ClassName: PosterManagerService
 * @Description: TODO 海报管理处理层
 * @author: zhangxiao
 * @date: 2020年11月30日 下午7:29:38
 */
@Service
public class PosterManagerService {
    @Resource
    private PosterManagerDao dao;
    @Resource
    private BlackPersonDao blackPersonDao;

    /**
     * 分页查询海报管理信息
     * @param pageQuery
     * @return
     */
    public PageList<PosterManager> queryPageList(PageQuery<PosterManager> pageQuery) {
        PageList<PosterManager> result = new PageList<PosterManager>();
        Integer itemCount = dao.count(pageQuery);
        PageQueryWrapper<PosterManager> wrapper = new PageQueryWrapper<PosterManager>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<PosterManager> list = dao.queryPageList(wrapper);
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }

    /**
     * 查询分页列表
     * @param pageQuery
     * @return
     */
    public PageList<PosterManager> findPageList(PageQuery<PosterManager> pageQuery) {
        PageList<PosterManager> result = new PageList<>();
        Integer itemCount = dao.myCount(pageQuery);
        PageQueryWrapper<PosterManager> wrapper = new PageQueryWrapper<>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<PosterManager> list = dao.findMyCount(wrapper);
        for(PosterManager p: list) {
            if(p != null && StringUtils.isNotEmpty(p.getStudyOpenId())) {
                BlackPerson blackPerson = new BlackPerson();
                blackPerson.setOpenId(p.getStudyOpenId());
                List<BlackPerson> blackList = blackPersonDao.findByParam(blackPerson);
                if(blackList != null && blackList.size() > 0) {
                    p.setIsBlack("1");
                }
            }
        }
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }


    /**
     * 新增海报管理信息
     * @param posterManager
     */
    public void doAdd(PosterManager posterManager) {
        dao.insert(posterManager);
    }

    /**
     * 根据参数获取海报管理信息数量
     * @param posterManager
     * @return
     */
    public Integer count(PosterManager posterManager) {
        PageQuery<PosterManager> pageQuery = new PageQuery<>();
        pageQuery.setQuery(posterManager);
        return dao.count(pageQuery);
    }

    /**
     * 根据id查询海报管理信息
     * @param id
     * @return
     */
    public PosterManager findById(Long id) {
        return dao.get(id);
    }

    /**
     * 根据传参获取海报管理列表信息
     * @param posterManager
     * @return
     */
    public List<PosterManager> findByParam(PosterManager posterManager) {
        return dao.findByParam(posterManager);
    }

    /**
     * 编辑海报管理信息
     * @param posterManager
     */
    public void doEdit(PosterManager posterManager) {
        dao.update(posterManager);
    }

    /**
     * 删除海报管理信息
     * @param posterManager
     */
    public void doDelete(PosterManager posterManager) {
        posterManager.setStatus("0");
        dao.update(posterManager);
    }

    /**
     * 查询扫码统计
     * @param noOpenIds 不查询的openid
     * @return
     */
    public List<Map<String,Object>> findCount(String noOpenIds) {
        return dao.findCount(noOpenIds);
    }

    /**
     * 查询前一天的扫码数量
     * @return
     */
    public Integer findBeforeDayScanNum() {
        return dao.findBeforeDayScanNum();
    }

    /**
     * 查询当天的扫码数量
     * @return
     */
    public Integer findNowDayScanNum() {
        Integer num = dao.findNowDayScanNum();
        return num == null ? 0 : num;
    }

    /**
     * 分组统计扫码信息
     * @param wrapper
     * @return
     */
    public List<Map<String, Object>> findGroupByParam(PageQuery<PosterManager> wrapper) {
        return dao.findGroupByParam(wrapper);
    }
}
